package com.nanhang.controller;



import com.nanhang.client.OrderClient;
import com.nanhang.entity.Order;
import com.nanhang.entity.User;
import com.nanhang.service.IUserService;
import com.nanhang.util.PageResult;
import com.nanhang.util.R;
import com.nanhang.Uservo.uservo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ym
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/user")
@RefreshScope
@Validated
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private OrderClient client;

    @Value("${student.sname}")
    private String sname;
    @Value("${student.age}")
    private int age;

    @GetMapping("/list")
    public R list(){
        return R.ok().data("list",userService.list());
    }

    @GetMapping("/getConfig")
    public R getConfig(){
        Map<String,Object> map = new HashMap<>();
        map.put("sname",sname);
        map.put("age",age);
        return R.ok().data("map",map);
    }

    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) uservo vo){
        PageResult<User> pr = userService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }
    @PostMapping("/save")
    public R save(@Validated @RequestBody User user){
        boolean userflag= false;
        R orderflag = null;
        Order order = new Order();
        order.setBuyerNick(user.getNickName());
        order.setReceiverMobile(user.getPhone());
        order.setReceiver(user.getName());
        try {
            userflag = userService.save(user);
            if (userflag) {
                orderflag = client.save(order);
                if (orderflag.getCode()==200) {
                    return R.ok().message("添加成功！");
                } else {
                    userService.removeById(user);
                    return R.error().message("订单保存失败");
                }
            } else {
                return R.error().message("用户保存失败");
            }
        } catch (Exception f) {
            if (userflag) {
                userService.removeById(user);
            }
            if (orderflag != null && orderflag.getCode() == 200) {
                client.deleteone(order);
            }
            return R.error().message("添加失败：" + f.getMessage());
        }
    }
    @PutMapping("/update")
    public R update(@RequestBody User user){

        return userService.updateById(user) ? R.ok().message("修改成功！") : R.error().message("修改失败！");
    }
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] shuzu=ids.split(",");
        List<String> sids = Arrays.asList(shuzu);


        return userService.removeByIds(sids) ? R.ok().message("删除成功！") : R.error().message("删除失败！");
    }

}
